<?php


namespace app\common\library\jobs;


use app\common\model\User;
use app\common\model\UserGroup;
use app\index\model\wanlshop\Shop;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\queue\Job;

class Goods extends Base
{
    public function copy(Job $job, $data){

        if ($job->attempts() >3){
            $this->failed($job->getName(),$data);
            $job->delete();
            return;
        }
        Db::startTrans();
        try {
            $row = model('app\index\model\wanlshop\Goods')->where([
                'parent_id' => $data['id'],
                'shop_id' => $data['shop_id']
            ])->find();
            $shop = Shop::get($data['shop_id']);
            if(!$row){
                $goods = model('app\index\model\wanlshop\Goods')->where([
                    'id' => $data['id'],
                    'shop_id' => 0
                ])->find();//->toArray();
                if(empty($goods)) {
                    Db::commit();
                    $job->delete();
                    return null;
                }
                $goods = $goods->toArray();
                $goods['parent_id'] = $goods['id'];
                $goods['shop_id'] = $data['shop_id'];
                $goods['freight_id'] = 0;
                $goods['grounding'] = 1;
                $goods['status'] = 'hidden';
                unset($goods['id']);
                unset($goods['shop_category_id']);
                $goodsModel = new \app\index\model\wanlshop\Goods();
                $newGoods = $goodsModel->allowField(true)->save($goods);
                $newGoodsId = $goodsModel->id;
                // 查询SKU
                $skuItem = model('app\index\model\wanlshop\GoodsSku')
                    ->where(['goods_id' => $data['id'], 'state' => 0])
                    ->order('sort asc')
                    ->select();
                $newSkuItem = [];
                $userGroup = UserGroup::where('status','normal')->select();
                foreach (collection($skuItem)->toArray() as $item){
                    $group_price = is_array($item['group_price']) ? $item['group_price'] :  json_decode($item['group_price'],true);
                    $supply_price = $item['supply_price'];
                    foreach ($group_price as $group){
                        if($shop['group_id'] == $group['group_id']){
                            $supply_price = $group['price'];
                        }
                    }
                    $userGroupPrice = [];
                    foreach ($userGroup as $val){
                        $userGroupPrice[] = [
                            'group_id' => $val['id'],
                            'price' => bcmul($supply_price,1.2,2),
                        ];
                    }
                    $item['supply_price'] = $supply_price;
                    $item['goods_id'] = $newGoodsId;
                    $item['group_price'] = json_encode($userGroupPrice);
                    unset($item['id']);
                    $newSkuItem[] = $item;
                }
                model('app\index\model\wanlshop\GoodsSku')->allowField(true)->saveAll($newSkuItem);
                // 查询SPU
                $spuItem = model('app\index\model\wanlshop\GoodsSpu')
                    ->where(['goods_id'=>$data['id']])
                    ->select();
                $newSpuItem = [];
                foreach (collection($spuItem)->toArray() as $item){
                    $item['goods_id'] = $newGoodsId;
                    unset($item['id']);
                    $newSpuItem[] = $item;
                }
                model('app\index\model\wanlshop\GoodsSpu')->allowField(true)->saveAll($newSpuItem);
            }
            Db::commit();
            $job->delete();
        } catch (Exception $e){
            // 队列执行失败
            Db::rollback();
            \think\Log::write('queue-' . get_class() . '-close' . '：执行失败，错误信息：' . $e->getMessage());
        }catch (PDOException $e){
            Db::rollback();
            \think\Log::write('queue-' . get_class() . '-close' . '：执行失败，错误信息：' . $e->getMessage());
        }
    }

    public function pricing(Job $job, $data){
        \think\Log::write('准备执行--pricing--'.json_encode($data));
        if ($job->attempts() >3){
            $this->failed($job->getName(),$data);
            $job->delete();
            return;
        }
        try{
            model('app\index\model\wanlshop\Goods')->updateGoodsSku($data['goods_id']);
            $job->delete();
        }catch (Exception $e){
            // 队列执行失败
            \think\Log::write('queue-' . get_class() . '-close' . '：执行失败，错误信息：' . $e->getMessage());
        }catch (PDOException $e){
            \think\Log::write('queue-' . get_class() . '-close' . '：执行失败，错误信息：' . $e->getMessage());
        }

    }
}